package com.xms.ribbonbutton.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.dependency.XmsDependencyException;
import com.xms.ribbonbutton.entity.RibbonButton;
import com.xms.schema.event.EntityMetaDeletedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * <p>
 * 实体删除后，级联删除按钮
 * </p>
 *
 * @author migo
 * @since 2021/10/26
 */
@Service
public class RibbonButtonCascadeDeleter {
    private IRibbonButtonDeleterService _ribbonButtonDeleterService;

    @Autowired
    public void RibbonButtonCascadeDeleter(IRibbonButtonDeleterService ribbonButtonDeleterService) {
        _ribbonButtonDeleterService = ribbonButtonDeleterService;
    }

    @Async
    @EventListener
    public void onApplicationEvent(EntityMetaDeletedEvent event) throws XmsDependencyException, SQLException {
        _ribbonButtonDeleterService.deleteMany(new LambdaQueryWrapper<RibbonButton>()
                .eq(RibbonButton::getEntityid, event.source.getEntityid()));
    }
}
